package com.lemon.core.common.lang.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author lottery
 * @ClassName CommandExecUtils
 * @desc
 * @Date 2018/11/19 17:51
 * @Version 1.0
 **/
public class CommandExecUtils {

    private static Runtime runtime;

    private static CommandExecUtils commandExecUtils;

    public static CommandExecUtils getInstance() {
        if (commandExecUtils == null) {
            synchronized (CommandExecUtils.class) {
                if (commandExecUtils == null) {
                    commandExecUtils = new CommandExecUtils();
                }
            }
        }
        return commandExecUtils;
    }

    private CommandExecUtils() {
        runtime = Runtime.getRuntime();
    }

    /**
     * linux exec ./file
     * @param filePath
     */
    public static String execFile(String filePath) {
        try {
            return getOutputContent(Runtime.getRuntime().exec(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void exec(String cmd) {
        if (SystemEnvUtils.isLinuxOS()) {
            shell(cmd, false);
        } else {
            dos(cmd, false);
        }
    }

    public static String exec(String cmd, boolean isReturn) {
        if (SystemEnvUtils.isLinuxOS()) {
            return shell(cmd, true);
        } else {
            return dos(cmd, true);
        }
    }

    private static String dos(String cmd, boolean isReturn) {
        try {
            String [] dos = {"cmd", "/C", cmd};
            Process process = Runtime.getRuntime().exec(dos);
            int exitVal = process.waitFor();
            if (exitVal != 0) {
                //System.out.println("执行失败!");
            } else {
                //System.out.println("执行成功!");
            }

            if (isReturn) {
                return getOutputContent(process);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String shell(String cmd, boolean isReturn) {
        try {
            String[] shell = {"/bin/sh", "-c", cmd};
            Process process = Runtime.getRuntime().exec(shell);
            int exitVal = process.waitFor();
            if (exitVal != 0) {
                //System.out.println("执行失败!");
            } else {
                //System.out.println("执行成功!");
            }

            if (isReturn) {
                return getOutputContent(process);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getOutputContent(Process process) {
        StringBuffer output = new StringBuffer();
        InputStreamReader isr = new InputStreamReader(process.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(isr);
        String line = null;
        try {
            while((line = bufferedReader.readLine()) != null) {
                output.append(line).append(" ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return output.toString();
    }
}
